//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using NopSolutions.NopCommerce.BusinessLogic.Audit;
using NopSolutions.NopCommerce.BusinessLogic.Caching;
using NopSolutions.NopCommerce.BusinessLogic.Configuration.Settings;
using NopSolutions.NopCommerce.BusinessLogic.CustomerManagement;
using NopSolutions.NopCommerce.BusinessLogic.Data;
using NopSolutions.NopCommerce.BusinessLogic.Directory;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;
using NopSolutions.NopCommerce.BusinessLogic.Orders;
using NopSolutions.NopCommerce.BusinessLogic.Products.Attributes;
using NopSolutions.NopCommerce.BusinessLogic.Promo.Discounts;
using NopSolutions.NopCommerce.BusinessLogic.Tax;
using NopSolutions.NopCommerce.Common;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.BusinessLogic.Alesko
{
    /// <summary>
    /// PaymentType service
    /// </summary>
    public partial class PaymentTypeService : IPaymentTypeService
    {
        #region Constants

        private const string DELIVERIES_BY_ID_KEY = "Nop.PaymentType.id-{0}";
        private const string DELIVERIES_PATTERN_KEY = "Nop.PaymentType.";

        #endregion

        #region Fields

        /// <summary>
        /// Object context
        /// </summary>
        private readonly NopObjectContext _context;

        /// <summary>
        /// Cache manager
        /// </summary>
        private readonly ICacheManager _cacheManager;

        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="context">Object context</param>
        public PaymentTypeService(NopObjectContext context)
        {
            this._context = context;
            this._cacheManager = new NopRequestCache();
        }

        #endregion

        #region Methods

        #region PaymentType methods

        /// <summary>
        /// Deletes a PaymentType method
        /// </summary>
        /// <param name="PaymentTypeId">The PaymentType method identifier</param>
        public void DeletePaymentType(int PaymentTypeId)
        {
            var PaymentType = GetPaymentTypeById(PaymentTypeId);
            if (PaymentType == null)
                return;

            if (!_context.IsAttached(PaymentType))
                _context.PaymentTypes.Attach(PaymentType);
            _context.DeleteObject(PaymentType);
            _context.SaveChanges();

            if (this.CacheEnabled)
            {
                _cacheManager.RemoveByPattern(DELIVERIES_PATTERN_KEY);
            }
        }

        /// <summary>
        /// Gets a PaymentType method
        /// </summary>
        /// <param name="PaymentTypeId">The PaymentType method identifier</param>
        /// <returns>PaymentType method</returns>
        public PaymentType GetPaymentTypeById(int PaymentTypeId)
        {
            if (PaymentTypeId == 0)
                return null;

            string key = string.Format(DELIVERIES_BY_ID_KEY, PaymentTypeId);
            object obj2 = _cacheManager.Get(key);
            if (this.CacheEnabled && (obj2 != null))
            {
                return (PaymentType)obj2;
            }

            var query = from sm in _context.PaymentTypes
                        where sm.PaymentTypeId == PaymentTypeId
                        select sm;
            var PaymentType = query.SingleOrDefault();

            if (this.CacheEnabled)
            {
                _cacheManager.Add(key, PaymentType);
            }
            return PaymentType;
        }

        /// <summary>
        /// Gets all PaymentType methods
        /// </summary>
        /// <returns>PaymentType method collection</returns>
        public List<PaymentType> GetAllPaymentTypes()
        {
          var query = from sm in _context.PaymentTypes
                      orderby sm.DisplayOrder
                      select sm;

          var PaymentTypes = query.ToList();
          return PaymentTypes;

        }

        /// <summary>
        /// Inserts a PaymentType method
        /// </summary>
        /// <param name="PaymentType">PaymentType method</param>
        public void InsertPaymentType(PaymentType PaymentType)
        {
            if (PaymentType == null)
                throw new ArgumentNullException("PaymentType");

            PaymentType.PaymentTypeName = CommonHelper.EnsureNotNull(PaymentType.PaymentTypeName);
            PaymentType.PaymentTypeName = CommonHelper.EnsureMaximumLength(PaymentType.PaymentTypeName, 100);
            PaymentType.Description = CommonHelper.EnsureNotNull(PaymentType.Description);
            
            _context.PaymentTypes.AddObject(PaymentType);
            _context.SaveChanges();

            if (this.CacheEnabled)
            {
              _cacheManager.RemoveByPattern(DELIVERIES_PATTERN_KEY);
            }
        }

        /// <summary>
        /// Updates the PaymentType method
        /// </summary>
        /// <param name="PaymentType">PaymentType method</param>
        public void UpdatePaymentType(PaymentType PaymentType)
        {
            if (PaymentType == null)
                throw new ArgumentNullException("PaymentType");

            PaymentType.PaymentTypeName = CommonHelper.EnsureNotNull(PaymentType.PaymentTypeName);
            PaymentType.PaymentTypeName = CommonHelper.EnsureMaximumLength(PaymentType.PaymentTypeName, 100);
            PaymentType.Description = CommonHelper.EnsureNotNull(PaymentType.Description);


            if (!_context.IsAttached(PaymentType))
                _context.PaymentTypes.Attach(PaymentType);

            _context.SaveChanges();

            if (this.CacheEnabled)
            {
              _cacheManager.RemoveByPattern(DELIVERIES_PATTERN_KEY);
            }
        }
        #endregion

        #endregion

        #region Properties
        
        /// <summary>
        /// Gets a value indicating whether cache is enabled
        /// </summary>
        public bool CacheEnabled
        {
            get
            {
                return IoC.Resolve<ISettingManager>().GetSettingValueBoolean("Cache.PaymentTypeManager.CacheEnabled");
            }
        }
        #endregion
    }
}
